/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.util;


import com.mysql.jdbc.Statement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.logging.Level;
import java.util.logging.Logger;



/**
 *
 * @author Usuario
 */
public class UtilidadesBD {

	private static final String IP="Usuario-HP";
	private static final String PUERTO ="3306";
	private static final String USUARIO = "root";
	private static final String CONTRA ="12345";
 
    public static Connection conexionMySQL() {
        Connection con = null;

        try {
        	
        	
        	 String conexionMySQLURL = "jdbc:mysql://"+IP+"/toke:" + PUERTO;
         	
         	  
         	 
         	  	
         	  Class.forName("com.mysql.jdbc.Driver");  
         	  con = DriverManager.getConnection(conexionMySQLURL,USUARIO, CONTRA);
         	  
         	 
         	  con.close();            	  
        	
        	
            //Class.forName("com.mysql.jdbc.Driver");
            //con = (Connection) DriverManager.getConnection("jdbc:mysql://Usuario-HP:3306/toke", "root", "12345");
            Logger.getLogger(UtilidadesBD.class.getName()).log(Level.INFO, "Conexion establecida");
        } catch (SQLException ex) {
            Logger.getLogger(UtilidadesBD.class.getName()).log(Level.SEVERE, "ERROR", ex);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            Logger.getLogger(UtilidadesBD.class.getName()).log(Level.SEVERE, "ERROR", e);
        }

        return con;
    }
    
    public static boolean cerrarConexionMysql(Connection con){
        boolean resultado = false;
        try {
            con.close();
            resultado = true;
        } catch (SQLException ex) {
            Logger.getLogger(UtilidadesBD.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resultado;
    }
    

    public static ResultSet selectMySQL(String sql,Connection con) {
        ResultSet rs = null;
        try {
            Statement stmt = (Statement) con.createStatement();
            rs = stmt.executeQuery(sql);
            Logger.getLogger(UtilidadesBD.class.getName()).log(Level.INFO, "Consulta: " + sql);
        } catch (SQLException ex) {
            Logger.getLogger(UtilidadesBD.class.getName()).log(Level.SEVERE, "ERROR", ex);
        } catch (Exception e) {
            Logger.getLogger(UtilidadesBD.class.getName()).log(Level.SEVERE, "ERROR", e);
        }
        return rs;
    }

    public static int insertMySQL(String sql) {
        int resultado = 0;
        try {

            Connection con = conexionMySQL();
            Statement stmt = (Statement) con.createStatement();
            resultado = stmt.executeUpdate(sql);
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(UtilidadesBD.class.getName()).log(Level.SEVERE, "ERROR", ex.getErrorCode());
            Logger.getLogger(UtilidadesBD.class.getName()).log(Level.SEVERE, "ERROR", ex.getSQLState());
            Logger.getLogger(UtilidadesBD.class.getName()).log(Level.SEVERE, "ERROR", ex.getStackTrace());
        } catch (Exception e) {
            Logger.getLogger(UtilidadesBD.class.getName()).log(Level.SEVERE, "ERROR", e);
        }

        return resultado;
    }
}
